package com.focusai.efairy.ui.adapter;

import android.content.Context;
import android.view.View;

import com.focusai.efairy.R;
import com.focusai.efairy.model.location.Location;
import com.focusai.efairy.ui.adapter.base.BaseRecyclerAdapter;
import com.focusai.efairy.ui.adapter.base.BaseRecyclerViewHolder;

/**
 * Created by luo_guangwen on 2017/9/6.
 */

public class SelectLocationAdapter extends BaseRecyclerAdapter<Location> {

    private int selectPosition = -1;
    private OnItemLayoutClickListener listener;
    public SelectLocationAdapter(Context context) {
        super(context);
    }

    @Override
    protected int getItemLayoutId() {
        return R.layout.item_selecte_lacation;
    }

    @Override
    protected void onBindBaseRecyclerViewHolder(BaseRecyclerViewHolder holder, final int position) {
        final Location item = mList.get(position);
        holder.setText(R.id.text_address,item.getAddress());
        holder.setText(R.id.text_street,item.getStreet());
        if (selectPosition == position){
            holder.getView(R.id.iv_select).setVisibility(View.VISIBLE);
            holder.getView(R.id.item_llayout).setBackgroundColor(mContext.getResources().getColor(R.color.white));
        }else {
            holder.getView(R.id.iv_select).setVisibility(View.GONE);
            holder.getView(R.id.item_llayout).setBackgroundColor(mContext.getResources().getColor(R.color.transparent));
        }
        if (this.listener != null){
            holder.getView(R.id.item_llayout).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    selectPosition = position;
                    listener.onItemClick(item,position);
                    notifyDataSetChanged();
                }
            });
        }
    }
    public void clearList(){
        selectPosition = -1;
    }

    public void setListener(OnItemLayoutClickListener listener) {
        this.listener = listener;
    }

    public int getSelectPosition() {
        return selectPosition;
    }

    public void setSelectPosition(int selectPosition) {
        this.selectPosition = selectPosition;
    }

    public interface OnItemLayoutClickListener{
        void onItemClick(Location location,int position);
    }
}
